//   MVCWin32 (Menu.cpp)
//
//   Copyright 2010 Eric Llewellyn
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#include "Menu.h"

#include <windows.h>

namespace MVCWin32
{
	//Static
	std::map<HMENU, Menu*> Menu::menuMap;

	//Static
	std::vector<MenuItem*> Menu::items;

	Menu::Menu()
	{
		handle = CreateMenu();
		Menu::menuMap[handle] = this;
	}

	void Menu::OnWMCommand(WORD id)
	{
		if(items[id]->eventPtr != 0 && items[id]->target != 0)
			(items[id]->target->*items[id]->eventPtr)(items[id]);
	}

	void Menu::AddMenuItem(MenuItem* item)
	{
		AppendMenu(handle,MF_STRING,items.size(),item->GetName());
		items.push_back(item);
	}

	void Menu::AddSubMenu(Menu* menu,LPCTSTR name)
	{
		AppendMenu(handle,MF_POPUP,(UINT_PTR)menu->handle,name);
	}

	void Menu::Init()
	{
	}

	HMENU Menu::GetHandle()
	{
		return handle;
	}

	void Menu::SetHandle(HMENU handle)
	{
		this->handle = handle;
	}
}
